package com.fnzn.entranceGuard.rest.usrCloud;

import cn.usr.UsrCloudMqttClientAdapter;
import org.eclipse.paho.client.mqttv3.MqttException;

/**
 * @author wf_sxf@163.com
 * @date 2018/5/24 19:04
 */
public class usrTest {

    private static String device_id="00012950000000000001";
    static String username="616049292";
    static String password = "shi895623";

    public static void main(String[] args) {
        try {
            SubscribeForDevId();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 链接透传云
     * @throws MqttException
     */
    public static void connect() throws MqttException {
    /* 1.初始化客户端适配 */
        UsrCloudMqttClientAdapter clientAdapter = new ClientAdapter();

    /* 2.初始化客户端回调适配 */
        ClinetCallbackAdapter clinetCallbackAdapter = new ClinetCallbackAdapter();

    /* 3.客户端设置回调 */
       clientAdapter.setUsrCloudMqttCallback(clinetCallbackAdapter);

    /* 4.进行连接 */
        clientAdapter.Connect("testuser", "testuser");
    }

    /**
     * 利用凭据
     * @throws MqttException
     */
    public void ConnectByToken() throws MqttException {

  /* 1.初始化客户端适配 */
        ClientAdapter clientAdapter = new ClientAdapter();

  /* 2.初始化客户端回调适配 */
        ClinetCallbackAdapter clinetCallbackAdapter = new ClinetCallbackAdapter();

  /* 3.客户端设置回调 */
        clientAdapter.setUsrCloudMqttCallback(clinetCallbackAdapter);

  /* 4.进行连接 */
        clientAdapter.ConnectByToken("testuser", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJyb2NrIiwidWlkIjo4MzE4LCJ.....");

    }

    public static boolean disConnect() throws MqttException {

  /* 1.初始化客户端适配 */
        ClientAdapter clientAdapter = new ClientAdapter();

  /* 2.初始化客户端回调适配 */
        ClinetCallbackAdapter clinetCallbackAdapter = new ClinetCallbackAdapter();

  /* 3.客户端设置回调 */
        clientAdapter.setUsrCloudMqttCallback(clinetCallbackAdapter);
  /* 4.取消连接 */
        return clientAdapter.DisConnectUnCheck();

    }
    /**
     * 订阅消息
     */
    public static  void SubscribeForDevId() throws MqttException, InterruptedException {

  /* 1.初始化客户端适配 */
        ClientAdapter clientAdapter = new ClientAdapter();

  /* 2.初始化客户端回调适配 */
        ClinetCallbackAdapter clinetCallbackAdapter = new ClinetCallbackAdapter();

  /* 3.客户端设置回调 */
        clientAdapter.setUsrCloudMqttCallback(clinetCallbackAdapter);


  /* 4.进行连接 */
        clientAdapter.Connect(username, password);
        Thread.sleep(3000);
  /* 5.订阅消息(单个设备)*/
        clientAdapter.SubscribeForDevId(device_id);
    }

    /**
     * 订阅账号下的所有设备
     * @throws MqttException
     */
    public void SubscribeForUsername() throws MqttException {

  /* 1.初始化客户端适配 */
        ClientAdapter clientAdapter = new ClientAdapter();

  /* 2.初始化客户端回调适配 */
        ClinetCallbackAdapter clinetCallbackAdapter = new ClinetCallbackAdapter();

  /* 3.客户端设置回调 */
        clientAdapter.setUsrCloudMqttCallback(clinetCallbackAdapter);

  /* 4.进行连接 */
        clientAdapter.Connect("testuser", "testuser");

  /* 5.订阅消息(账户的所有设备) */
        clientAdapter.SubscribeForUsername();
    }
    /**
     * 取消订阅 单个
     */
    public void DisSubscribeforDevId() throws MqttException {

  /* 1.初始化客户端适配 */
        ClientAdapter clientAdapter = new ClientAdapter();

  /* 2.初始化客户端回调适配 */
        ClinetCallbackAdapter clinetCallbackAdapter = new ClinetCallbackAdapter();

  /* 3.客户端设置回调 */
        clientAdapter.setUsrCloudMqttCallback(clinetCallbackAdapter);

  /* 4.进行连接 */
        clientAdapter.Connect("testuser", "testuser");

  /* 5.取消订阅消息(单个设备) */
        clientAdapter.DisSubscribeforDevId("1234");

    }
    /**
     * 取消订阅全部
     */
    public void DisSubscribeforuName() throws MqttException {
  /* 1.初始化客户端适配 */
        ClientAdapter clientAdapter = new ClientAdapter();

  /* 2.初始化客户端回调适配 */
        ClinetCallbackAdapter clinetCallbackAdapter = new ClinetCallbackAdapter();

  /* 3.客户端设置回调 */
        clientAdapter.setUsrCloudMqttCallback(clinetCallbackAdapter);

  /* 4.进行连接 */
        clientAdapter.Connect("testuser", "testuser");

  /* 5.取消订阅消息(账户下所有设备) */
        clientAdapter.DisSubscribeforuName();

    }

    /**
     * 推送数据（向单个设备）
     * @throws MqttException
     */
    public static void publishForDevId() throws MqttException, InterruptedException {
  /* 1.初始化客户端适配 */
        ClientAdapter clientAdapter = new ClientAdapter();

  /* 2.初始化客户端回调适配 */
        ClinetCallbackAdapter clinetCallbackAdapter = new ClinetCallbackAdapter();

  /* 3.客户端设置回调 */
        clientAdapter.setUsrCloudMqttCallback(clinetCallbackAdapter);

  /* 4.进行连接 */
        clientAdapter.Connect(username, password);
        Thread.sleep(3000);

  /* 5.初始化测试数据 */
        byte[] data = { 0x55, 0x01, 0x12, 0x00 ,0x00 ,0x00 ,0x01 ,0x69 };

  /* 6.发送数据 */
        clientAdapter.publishForDevId(device_id, data);

    }
    public void publishForuName() throws MqttException {
  /* 1.初始化客户端适配 */
        ClientAdapter clientAdapter = new ClientAdapter();

  /* 2.初始化客户端回调适配 */
        ClinetCallbackAdapter clinetCallbackAdapter = new ClinetCallbackAdapter();

  /* 3.客户端设置回调 */
        clientAdapter.setUsrCloudMqttCallback(clinetCallbackAdapter);

  /* 4.进行连接 */
        clientAdapter.Connect("testuser", "testuser");

  /* 5.初始化测试数据 */
        byte[] data = { 0x00, 0x01, 0x02 };

  /* 6.发送数据 */
        clientAdapter.publishForuName(data);

    }

}
